// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import { Message } from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css';
import axios from 'axios'
//import store from './store'
import {initMenu} from './util/myutil'

Vue.config.productionTip = false
Vue.use(ElementUI)

Vue.prototype.$axios = axios
//axios.defaults.baseURL = 'http://localhost:8080'
//axios.defaults.timeout = 3000
axios.defaults.withCredentials = true //请求发送cookie
// 添加请求拦截器
axios.interceptors.request.use(function (config) {
	console.log('>> in axios.req-interceptor, re-config:', config);
    return config;
  }, function (error) {
    return Promise.reject(error);
  });
// 添加响应拦截器
axios.interceptors.response.use(function (response) {
	console.log('>> in axios.resp-interceptor, response:', response);
	if(!response.data.success){
		let rs = response.data;
		console.log('errCode:', rs.errCode, 'errMsg:', rs.errMsg);
		Message({type:'error', message:rs.errMsg, duration:2000, 
			onClose: () => {
				//登录session失效
				if(rs.errCode=='login02'){ 
					//window.location.href = '/';
					console.log('>> current route path pass to Login:', router.currentRoute.path);
					router.push({path:'/', query:{redirect:router.currentRoute.path}});
				}
			}
		});
	}
	return response;
  }, function (error) {
		console.log('>> in axios.resp-err-interceptor, error:', error);
		Message({showClose: true, message: error, type: 'error'});
    return Promise.reject(error);
  });

//路由全局守卫 for URL跳转（变化）拦截
router.beforeEach((to, from, next) => { 
	//console.log(to, from, next) //
	if(to.name=='Login'){ //本身就是登录页，就不用验证登录session了
		next()
		return
	}
	let loginSession = JSON.parse(sessionStorage.getItem('loginSession'))
	// let loginSession = store.state.loginSession //登录成功时加入 store 的
	if(!loginSession){ //没有登录/登录过期
		next({path:'/', query:{redirect:to.path}})
	}else{
		initMenu() //初始化菜单数据
		next()
	}
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
	//store,
  components: { App },
  template: '<App/>'
})
